<template>
    <el-card>
        <template #header>
            <div class="card-header">
                <span>用户列表</span>
            </div>
        </template>
        <el-table :data="form" style="width: 100%">
            <el-table-column prop="id" label="ID" width="90" />
            <el-table-column prop="avatar" label="头像" width="180">
                <template #default="scope">
                    <div class="demo-basic--circle">
                        <el-avatar :size="50" :src="scope.row.avatar" />
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="username" label="账户" width="180" />
            <el-table-column prop="fullname" label="姓名" />
            <el-table-column prop="sex" label="性别" />
            <el-table-column prop="tel" label="电话" />
            <el-table-column prop="role_name" label="角色">
                <template #default="scope">
                    <el-tag :type="scope.row.role_name=='超级管理员'?'danger':'primary'">
                        {{scope.row.role_name}}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="180">
                <template #default="scope">
                    <el-button type="primary" @click="edit(scope.row.id,scope.$index)">编辑</el-button>
                    <el-button type="danger" @click="handleRemove(scope.row.id,scope.$index)">删除</el-button>
                </template>

            </el-table-column>
        </el-table>
    </el-card>
    <el-dialog v-model="dialogVisible" title="修改信息" width="30%" :before-close="handleClose">
        <el-form :model="userForm" label-width="60px">
            <el-form-item label="账户">
                <el-input v-model="userForm.username" disabled />
            </el-form-item>
            <el-form-item label="姓名">
                <el-input v-model="userForm.fullname" />
            </el-form-item>
            <el-form-item label="性别">
                <el-radio-group v-model="userForm.sex">
                    <el-radio label="男" />
                    <el-radio label="女" />
                </el-radio-group>
            </el-form-item>
            <el-form-item label="手机">
                <el-input v-model="userForm.tel" />
            </el-form-item>
            <el-form-item label="头像">
                <el-upload class="avatar-uploader" action="http://localhost:3003/upload/common" :show-file-list="false"
                    :headers="headers" accept=".jpeg,.png,.jpg" :on-success="handleAvatarSuccess" :data="extra"
                    :on-error="handleUploadError" :before-upload="handleBeforeUpload" >
                    <img v-if="userForm.avatar" :src="userForm.avatar" class="avatar" />
                    <el-icon v-else class="avatar-uploader-icon">
                        <Plus />
                    </el-icon>
                </el-upload>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="handleEdit">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script setup>
import admin from '@/api/admin'
// import { ElMessage } from 'element-plus';
import { ref } from 'vue'
let form = ref([]);
let userForm = ref({});
let loadList = async () => {
    let { status, msg, data } = await admin.list();
    if (status) {
        form.value = data
    }
}
loadList()
let dialogVisible = ref(false);
let formIndex = ref();
let edit = async (id, index) => {
    dialogVisible.value = true;
    formIndex.value = index
    let { status, msg, data } = await admin.detail({ id });
    if (status) {
        userForm.value = { ...data }
    }
}
let handleEdit = async () => {
    let { status, msg } = await admin.update({ ...userForm.value });
    if (status) {
        dialogVisible.value = false;
        form.value[formIndex.value] = { ...userForm.value }
        ElMessage.success(msg);
    }
}
let handleRemove = async (id, index) => {
    let { status, msg } = await admin.remove(id);
    if (status) {
        form.value.splice(index, 1);
        ElMessage.success(msg);
    }

}
</script>
<style scoped>
.editor-box {
    border: 1px solid #ccc;
    border-radius: 3px;
}

.el-input {
    width: 200px;
}

.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>